﻿using System;
using System.Collections.Generic;
using NHibernate;
using Symbonix.NorSoNet.Security.DAL.Entities;
using Symbonix.Library.Data.NHibernate;
using System.Collections;
using NHibernate.Criterion;
using System.Text;

namespace Symbonix.NorSoNet.Security.DAL.Repositories
{
    public partial class UserInfoDataRepository : BaseRepository<UserInfoData, Int32>
    {
        public IList GetAll(string searchText, string orderBy, bool isAscending, bool allowPaging, int pageIndex, int pageSize, out int totalItems)
        {
            totalItems = 0;
            int firstRow = (pageIndex - 1) * pageSize;
            int maxResult = pageSize;

            ICriteria criteria = GetCriteria().Add(Expression.Like("UserName", "%" + searchText + "%"));
            ICriteria dataCriteria =
                (allowPaging
                ? ((ICriteria)criteria.Clone()).SetFirstResult(firstRow).SetMaxResults(maxResult)
                : (ICriteria)criteria.Clone());
            if (!string.IsNullOrEmpty(orderBy))
                dataCriteria.AddOrder(new Order(orderBy, isAscending));
            ICriteria countCriteria = criteria.SetProjection(Projections.RowCount());
            IMultiCriteria multiCriteria = NHibernateHelper.CurrentSession.CreateMultiCriteria();
            multiCriteria.Add(dataCriteria);
            multiCriteria.Add(countCriteria);

            IList results = multiCriteria.List();
            IList items = (IList)results[0];
            totalItems = Convert.ToInt32(((IList)results[1])[0]);

            return items;
        }
    }
}
